package com.iterator.realm;



import com.iterator.bean.web.wy.admin.Admin;
import com.iterator.mapper.web.marketManager.RoleThreeLevelMapper;
import com.iterator.mapper.web.systemManager.AdminMapper;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CustomRealm extends AuthorizingRealm {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    RoleThreeLevelMapper roleThreeLevelMapper;
    /**
     * 认证方法
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        CustomToken token = (CustomToken)authenticationToken;
        String username = token.getUsername();
        String type = token.getType();
        List<String> strings;
        if("admin".equals(type)){
            //后台,查询管理员表
            strings = adminMapper.selectPasswordByUsername(username);
        }else{
            //前台,查询用户表
            strings = adminMapper.selectUserPasswordByUsername(username);
        }
        String credential = strings.size() >= 1? strings.get(0) : null;
        //第一个参数:获取得用户信息
        //第二个参数:根据用户信息查询出来密码信息
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, credential, this.getName());
        return authenticationInfo;
    }

    /**
     * 授权方法
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //getPrimaryPrincipal方法获取的是authenticationInfo中的principal参数信息
        String username = (String) principalCollection.getPrimaryPrincipal();
        SimpleAuthorizationInfo authenticationInfo = new SimpleAuthorizationInfo();

        List<Admin> admins = adminMapper.selectAllByUsername(username);
        if(admins == null){
            return null;
        }
        Admin admin = admins.get(0);
        String roleIds = admin.getRoleIds();
        String substring = roleIds.substring(1, roleIds.length()-1);
        String[] split = substring.split(",");
        ArrayList<Integer> integers = new ArrayList<>();
        if (!split[0].equals("")) {


            for (int i = 0; i < split.length; i++) {
                String trim = split[i].trim();
                integers.add(Integer.parseInt(trim));
            }
        }
        if (integers.size() == 0) {
            return null;
        }
        //查询授权信息
        List<String> permissions = roleThreeLevelMapper.selectPermissionsByRoleIds(integers);
        authenticationInfo.addStringPermissions(permissions);
        return authenticationInfo;
    }
}
